package com.hand.wfive.util;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.widget.Toast;

import androidx.core.content.FileProvider;

import com.hand.wfive.BuildConfig;
import com.hand.wfive.R;
import com.doris.media.picker.utils.MediaUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class ShareUtils {

    public static void share(Context context, String path, String type, String title) {
        File file = new File(path);
        if (file.exists()) {
            Intent share = new Intent(Intent.ACTION_SEND);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                Uri contentUri = FileProvider.getUriForFile(context,
                        BuildConfig.APPLICATION_ID + ".fileprovider", file);
                share.putExtra(Intent.EXTRA_STREAM, contentUri);
                share.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            } else {
                share.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
            }
            share.setType(type);
            share.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            share.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            context.startActivity(Intent.createChooser(share, title));
        } else {
            Toast.makeText(context, title + "不存在", Toast.LENGTH_SHORT).show();
        }
    }

    public static void share(Context context, List<String> files, String type, String title) {
        ArrayList<Uri> fileUris = loadShareUri(context, files, type);
        if (fileUris.isEmpty()) {
            Toast.makeText(context, title + "不存在", Toast.LENGTH_SHORT).show();
        } else {
            Intent share = new Intent();
            share.setType(type);
            share.setAction(Intent.ACTION_SEND_MULTIPLE);
            share.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            share.putParcelableArrayListExtra(Intent.EXTRA_STREAM, fileUris);
            context.startActivity(Intent.createChooser(share, title));
        }
    }

    private static ArrayList<Uri> loadShareUri(Context context, List<String> files, String type) {
        ArrayList<Uri> fileUris = new ArrayList<>();
        if (type.contains("application")) {
            for (String filePath : files) {
                Uri fileUri = loadShareUri(context, filePath);
                if (fileUri != null) fileUris.add(fileUri);
            }
        } else {
            for (String filePath : files) {
                Uri uri = type.contains("image") ? MediaUtils.findImageUriByPath(context, filePath) :
                        type.contains("video") ? MediaUtils.findVideoUriByPath(context, filePath) :
                                MediaUtils.findAudioUriByPath(context, filePath);
                if (uri != null) {
                    fileUris.add(uri);
                } else {
                    Uri fileUri = loadShareUri(context, filePath);
                    if (fileUri != null) fileUris.add(fileUri);
                }
            }
        }
        return fileUris;
    }

    private static Uri loadShareUri(Context context, String filePath) {
        File file = new File(filePath);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            if (file.exists()) {
                return FileProvider.getUriForFile(context,
                        BuildConfig.APPLICATION_ID + ".fileprovider", file);
            }
        } else {
            if (file.exists()) {
                return Uri.fromFile(file);
            }
        }
        return null;
    }

    /**
     * 分享图片
     */
    public static void sharePicture(Context context, String path) {
        share(context, path, "image/*", "分享图片");
    }

    public static void sharePicture(Context context, List<String> paths) {
        if (paths.size() == 1) {
            share(context, paths.get(0), "image/*", "分享图片");
        } else {
            share(context, paths, "image/*", "分享图片");
        }
    }

    /**
     * 分享视频
     */
    public static void shareVideo(Context context, String path) {
        share(context, path, "video/*", "分享视频");
    }

    public static void shareVideo(Context context, List<String> paths) {
        if (paths.size() == 1) {
            share(context, paths.get(0), "video/*", "分享视频");
        } else {
            share(context, paths, "video/*", "分享视频");
        }
    }

    /**
     * 分享音频
     */
    public static void shareAudio(Context context, String path) {
        share(context, path, "audio/*", "分享音频");
    }

    public static void shareAudio(Context context, List<String> paths) {
        if (paths.size() == 1) {
            share(context, paths.get(0), "audio/*", "分享音频");
        } else {
            share(context, paths, "audio/*", "分享音频");
        }
    }

    /**
     * 分享文件
     */
    public static void shareFile(Context context, String path) {
        shareFile(context, path, "分享文件");
    }

    public static void shareFile(Context context, String path, String title) {
        share(context, path, "application/*", title);
    }

    public static void shareFile(Context context, List<String> paths) {
        shareFile(context, paths, "分享文件");
    }

    public static void shareFile(Context context, List<String> paths, String title) {
        if (paths.size() == 1) {
            share(context, paths.get(0), "application/*", title);
        } else {
            share(context, paths, "application/*", title);
        }
    }

    /**
     * 分享文字
     */
    public static void shareString(Context context, String content) {
        Intent share = new Intent(Intent.ACTION_SEND);
        share.setType("text/plain");
        share.putExtra(Intent.EXTRA_TEXT, content);
        share.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(Intent.createChooser(share, context.getString(R.string.app_name)));
    }
}
